package com.rtx.edi.platform.department.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.department.po.PlDepartment;
import com.rtx.edi.model.department.dto.PlDepartmentDto;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.department.service.PlDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门信息表  前端控制器
 * </p>
 *
 * @author wq
 * @since 2023-03-05
 */
@Api(tags = {"部门管理"})
@RestController
@RequestMapping("/v1/plDepartment")
@CrossOrigin
public class PlDepartmentController {

    @Autowired
    private PlDepartmentService plDepartmentService;

    /**
     * 获取部门列表
     * @param plDepartmentDto
     * @return
     */
    @ApiOperation(value = "部门列表", notes = "分页获取部门列表")
    @PostMapping("selectPlDepartmentList")
    public EdiResponse selectPlDepartmentList(@RequestBody PlDepartmentDto plDepartmentDto){
        PlDepartment plDepartment = new PlDepartment();
        BeanUtils.copyProperties(plDepartmentDto,plDepartment);
        Page<PlDepartment> pageParam = new Page<>(plDepartmentDto.getPageNum(), plDepartmentDto.getPageSize());
        plDepartmentService.selectPlDepartmentList(pageParam,plDepartment);
        return  EdiResponse.success().data("page",pageParam);
    }

    /**
     * 根据ID获取部门
     * @param plDepartmentDto
     * @return
     */
    @ApiOperation(value = "查询部门", notes = "根据ID查询部门")
    @PostMapping("selectPlDepartmentById")
    public EdiResponse selectPlDepartmentById(@RequestBody PlDepartmentDto plDepartmentDto){
        return EdiResponse.success().data("item",plDepartmentService.getById(plDepartmentDto.getId()));
    }
    /**
     * 根据id删除
     * @param plDepartmentDto
     * @return
     */
    @ApiOperation(value = "删除部门", notes = "根据id删除部门", httpMethod = "POST")
    @PostMapping("deletePlDepartmentById")
    public EdiResponse deletePlDepartmentById(@RequestBody PlDepartmentDto plDepartmentDto) {
        return plDepartmentService.removeById(plDepartmentDto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }
    
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "批量删除部门", notes = "根据id列表删除部门", httpMethod = "POST")
    @PostMapping("deletePlApplicationByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlDepartmentDto> list) {
    	try {
    		plDepartmentService.deletePlApplicationByIds(list);
    		return EdiResponse.success();
    	} catch (Exception e) {
    		return EdiResponse.failed().returnMgr(e.getMessage());
    	}
    }

    /**
     * 新增部门
     * @param plDepartmentDto
     * @return
     */
    @ApiOperation(value = "新增部门")
    @PostMapping("addPlDepartment")
    public EdiResponse addPlDepartment(@RequestBody PlDepartmentDto plDepartmentDto){
        PlDepartment plDepartment = new PlDepartment();
        BeanUtils.copyProperties(plDepartmentDto,plDepartment);
        return  plDepartmentService.addPlDepartment(plDepartment) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 根据ID修改部门
     * @param plDepartmentDto
     * @return
     */
    @ApiOperation(value = "修改部门", notes = "根据ID修改部门")
    @PostMapping("editPlDepartmentById")
    public EdiResponse editPlDepartmentById(@RequestBody PlDepartmentDto plDepartmentDto){
        PlDepartment plDepartment = new PlDepartment();
        BeanUtils.copyProperties(plDepartmentDto,plDepartment);
        return plDepartmentService.updateById(plDepartment)  ? EdiResponse.success() : EdiResponse.failed();
    }
}
